package com.lht.leetcodeDemo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-07-03
 */
public class MaximumDepthofBinaryTree {

    @Test
    public void test(){
        TreeNode node=new TreeNode(3);
        node.left=new TreeNode(9);
        node.right=new TreeNode(20);
        node.left.left=null;
        node.left.right=null;
        node.right.left=new TreeNode(15);
        node.right.right=new TreeNode(7);

        maxDepth(node);
    }
    public int maxDepth(TreeNode root) {
        List<TreeNode> next = new ArrayList<>();
        int depth = 0;
        if(root==null){
            return 0;
        }
        next.add(root);
        while (!next.isEmpty()) {
            List<TreeNode> n = new ArrayList<>();
            for (TreeNode tree : next) {
                if (tree.left != null) {
                    n.add(tree.left);
                }
                if (tree.right != null) {
                    n.add(tree.right);
                }
            }
            next = n;
            depth++;
        }
        return depth;
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

}
